package 数据结构.栈和队列.Algorithm;

import java.util.Deque;
import java.util.LinkedList;

//利用栈实现队列
public class MyQueue {
    //定义栈
    Deque<Integer> deque1;
    Deque<Integer> deque2;

    public MyQueue() {
        deque1 = new LinkedList<>();
        deque2 = new LinkedList<>();
    }

    //这里的话就是要放进前面咯【因为队列是先进先出，所以应该放进队尾，
    // 但栈的push方法是将元素放在队头【实现先进后出，所以利用两个
    //
    public void push(int x) {
        deque2.push(x);
        while (!deque1.isEmpty()) {
            deque2.push(deque1.poll());
        }
        Deque<Integer> dequeTemp;
        dequeTemp = deque1;
        deque1 = deque2;
        deque2 = dequeTemp;
    }

    // 从队列的开头移除并返回元素
    //因为这里上面的push已经实现了队列实现栈，所以这里的pop即使是队头，但那个元素已经变成了后进的元素了
    public int pop() {
        return deque1.pop();
    }

    //返回队列开头的元素
    public int peek() {
        return deque1.peek();
    }

    public boolean empty() {
        return deque1.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue obj = new MyQueue();
        obj.push(1);
        obj.push(2);
        obj.push(3);
        obj.push(4);
        System.out.println(obj.pop());
        obj.push(5);
        System.out.println(obj.pop());
        System.out.println(obj.pop());
        System.out.println(obj.pop());
        int param_3 = obj.peek();
        boolean param_4 = obj.empty();
    }
}
